package com.android.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.android.R;
import com.android.SQL.MySQLiteOpenHelper;
import com.android.activity.adapter.DeliverysAdapter;
import com.android.bean.DeliveryAdr;
import com.android.model.Delivery;

import java.util.ArrayList;
import java.util.List;

public class DeliverActivity extends AppCompatActivity {

    MySQLiteOpenHelper myDbHelper;
    SQLiteDatabase db;
    private List<Delivery> deliList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delivery);

        Button new_addr = findViewById(R.id.btn_delivery_add);
        new_addr.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(DeliverActivity.this,
                        DetailDeliveryActivity.class);
                startActivity(intent);
            }
        });
        //查询数据
        myDbHelper = new MySQLiteOpenHelper(DeliverActivity.this);
        db = myDbHelper.getReadableDatabase();
        Cursor cursor = db.query(
                DeliveryAdr.DeliveryEntry.TABLE_NAME,
                null, null, null, null, null, null);

        int nameIndex = cursor.getColumnIndex(
                DeliveryAdr.DeliveryEntry.COLUMN_PEOPLE_NAME);
        int teleIndex = cursor.getColumnIndex(
                DeliveryAdr.DeliveryEntry.COLUMN_PEOPLE_TELE);
        int adrIndex = cursor.getColumnIndex(
                DeliveryAdr.DeliveryEntry.COLUMN_DETAIL_ADR);
        int swIndex = cursor.getColumnIndex(
                DeliveryAdr.DeliveryEntry.COLUMN_IS_RAW);

        while (cursor.moveToNext()) {
            Delivery delivers = new Delivery();

            String name = cursor.getString(nameIndex);
            String tel = cursor.getString(teleIndex);
            String adr = cursor.getString(adrIndex);
            String sw = cursor.getString(swIndex);


            delivers.setReceiver(name);
            delivers.setPhone(tel);
            delivers.setLocation(adr);
            deliList.add(delivers);
        }
        DeliverysAdapter deliAdapter = new DeliverysAdapter(
                DeliverActivity.this,
                R.layout.item_delivery,
                deliList);
        ListView lvdelisList = findViewById(R.id.rv_delivery);

        lvdelisList.setAdapter(deliAdapter);
    }
}